﻿@{
    ViewBag.Title = "套餐管理";
}

<div class="row">
    <div class="col-xs-12">
        <p>
            <div class="row">
                <div class="col-sm-6">
                    <div class="input-group-btn">
                        状态:
                        <select id="query-status">
                            <option value="1" selected="selected">正常</option>
                            <option value="2">已禁用</option>
                            <option value="3">已删除</option>
                        </select>
                        <button class="btn btn-sm btn-primary" id="btnNew">
                            <i class="icon-plus bigger-125"></i>创建
                        </button>
                        <button class="btn btn-sm btn-danger" id="btnDelete">
                            <i class="icon-remove bigger-125"></i>删除
                        </button>
                    </div>
                </div>
                <div class="col-sm-6 right">
                </div>
            </div>
        </p>
        <div class="table-responsive">
            <table id="businessTable" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="selectAll" /></th>
                        <th>Code</th>
                        <th>名称</th>
                        <th>价格</th>
                        <th>市场价格</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:80%;">
        <div class="modal-content">
            <div class="modal-header">
                @*<button type="button" class="close"
                            data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>*@
                <h4 class="modal-title" id="myModalLabel">
                    创建套餐
                </h4>
            </div>
            <div class="modal-body">
                <div class="tabbable">
                    <ul class="nav nav-tabs" id="myTab">
                        <li class="active">
                            <a data-toggle="tab" href="#newTabInfo">
                                <i class="green icon-home bigger-110"></i>
                                基本信息
                            </a>
                        </li>

                        <li>
                            <a data-toggle="tab" href="#newTabItems">
                                套餐项目
                                <span class="badge badge-danger" id="newPackageItemCount">0</span>
                            </a>
                        </li>
                        <li>
                            <a data-toggle="tab" href="#newTabPrice">
                                销售价格
                            </a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <div id="newTabInfo" class="tab-pane in active">
                            <form class="form-horizontal" role="form" id="formNew">
                                <input type="hidden" id="newId" />
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="newCode"> 编号 </label>
                                    <div class="col-sm-3">
                                        <input type="text" id="newCode" placeholder="输入编号..." class="col-xs-12 col-sm-12" />
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="newName"> 名称 </label>
                                    <div class="col-sm-3">
                                        <input type="text" id="newName" placeholder="输入名称..." class="col-xs-12 col-sm-12" />
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="newPackageAccessibility">客户限制</label>
                                    <div class="col-sm-3">
                                        <select id="newPackageAccessibility" class="col-xs-12 col-sm-12">
                                            <option value="1" selected="selected">全部客户</option>
                                            <option value="2">VIP客户</option>
                                            <option value="3">非VIP客户</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="space-4"></div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 起售日期 </label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <input class="form-control date-picker" id="newStartSaleTime" type="text" data-date-format="yyyy-mm-dd" />
                                            <span class="input-group-addon">
                                                <i class="icon-calendar bigger-110"></i>
                                            </span>
                                        </div>
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 停售日期 </label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <input class="form-control date-picker" id="newStopSaleTime" type="text" data-date-format="yyyy-mm-dd" />
                                            <span class="input-group-addon">
                                                <i class="icon-calendar bigger-110"></i>
                                            </span>
                                        </div>
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 过期日期 </label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <input class="form-control date-picker" id="newExpiringDate" type="text" data-date-format="yyyy-mm-dd" />
                                            <span class="input-group-addon">
                                                <i class="icon-calendar bigger-110"></i>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                                <div class="space-4"></div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 销售提成 </label>
                                    <div class="col-sm-3">
                                        <select id="newSalesCommissionType" class="col-xs-12 col-sm-12">
                                            <option value="1" selected="selected">无提成</option>
                                            <option value="2">固定金额</option>
                                            <option value="3">按比例</option>
                                        </select>
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 比例/金额 </label>
                                    <div class="col-sm-3">
                                        <input type="text" id="newSalesCommission" class="col-xs-12 col-sm-12" />
                                    </div>
                                </div>
                                <div class="space-4"></div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1">技师提成</label>
                                    <div class="col-sm-3">
                                        <select id="newTechnicianCommissionType" class="col-xs-12 col-sm-12">
                                            <option value="1" selected="selected">无提成</option>
                                            <option value="2">固定金额</option>
                                            <option value="3">按比例</option>
                                        </select>
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 比例/金额 </label>
                                    <div class="col-sm-3">
                                        <input type="text" id="newTechnicianCommission" class="col-xs-12 col-sm-12" />
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 售价 </label>
                                    <div class="col-sm-3">
                                        <input type="text" id="newPrice" placeholder="输入售价..." class="col-xs-12 col-sm-12" />
                                    </div>
                                </div>
                                <div class="space-4"></div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 描述 </label>
                                    <div class="col-sm-11">
                                        <textarea id="newDescription" placeholder="输入描述..." class="col-xs-12 col-sm-12" cols="3"></textarea>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div id="newTabItems" class="tab-pane">
                            <div class="input-group-btn">
                                <a href="javascript:void(0)" class="btn btn-sm btn-primary" id="btnNewServiceItem">
                                    <i class="icon-plus bigger-125"></i>添加服务项目
                                </a>
                                <a href="javascript:void(0)" class="btn btn-sm btn-info" id="btnNewVoucher">
                                    <i class="icon-plus bigger-125"></i>添加代金券
                                </a>
                            </div>
                            <div class="space-4"></div>
                            <div class="table-responsive">
                                <table id="newItemTable" class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th style="width:120px;">Name</th>
                                            <th>Type</th>
                                            <th style="width:70px;">金额</th>
                                            <th style="width:70px;">次数</th>
                                            <th style="width:70px;">市场价格</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                        <div id="newTabPrice" class="tab-pane">
                            <div class="table-responsive">
                                <table id="newPriceTable" class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th style="width:120px;">销售渠道</th>
                                            <th style="width:70px;">价格</th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    关闭
                </button>
                <button type="button" class="btn btn-primary" id="btnSubmitNew">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="pickerServiceItem" tabindex="-1" role="dialog"
     aria-labelledby="pickerServiceItemLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:60%;">
        <div class="modal-content">
            <div class="modal-header">
                @*<button type="button" class="close"
                            data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>*@
                <h4 class="modal-title" id="pickerServiceItemLabel">
                    选择服务项目
                </h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="input-group-btn">
                            过滤:
                            <input  id="editServiceItemFilter"/>
                            <button class="btn btn-sm btn-primary" id="btnQueryServiceItem">
                                <i class="icon-search bigger-125"></i>查询
                            </button>
                        </div>
                    </div>
                </div>
                <div class="space-2"></div>
                <div class="table-responsive">
                    <table id="itemTable" class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Name</th>
                                <th>工时单价</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    关闭
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

@section inlineScript
{
    <script type="text/javascript">
        var ASMS ={
            Constant:{
                PageAllLimit : 9999
            }
        };
        var oTable = null;
        var oTableServiceItem = null;
        var oTablePrice = null;
        var g_rowIndex = 1;
        var g_editingPackage = null;
        $(document).ready(function () {
            $('.date-picker').datepicker({ autoclose: true }).next().on(ace.click_event, function () {
                $(this).prev().focus();
            });
            $('#com-search-input').bind('keypress', function (event) {
                if (event.keyCode == "13") {
                    queryAdvertisementList();
                }
            });
            $('#com-search-icon').on('click', function () {
                queryAdvertisementList();
            });
            $('#query-status').on('change', function () {
                queryAdvertisementList();
                if ($('#query-status').val() == 3) {
                    $('#btnDelete').hide();
                } else {
                    $('#btnDelete').show();
                }
            });
            $('#selectAll').on('change', function () {
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        $(element).prop('checked', false);
                    } else {
                        $(element).prop('checked', true);
                    }
                });
            });
            $('#btnNew').on('click', function () {
                g_editingPackage = null;
                $('#formNew')[0].reset();
                $("#newItemTable tr:gt(0)").remove();
                $('#myModal').modal({
                    keyboard: true
                });
                queryPriceList();
            });
            $('#myModal').on('shown.bs.modal', function () {
                // 执行一些动作...
            })
            $('#btnDelete').on('click', function () {
                var idArray = [];
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        idArray.push($(element).attr('data-id'));
                    }
                });
                onDeleteMany(idArray);
            });
            $('#btnNewServiceItem').on('click', function () {
                $('#pickerServiceItem').modal();
                queryServiceItemList();
            });
            $('#btnNewVoucher').on('click', function () {
                var rowId = g_rowIndex++;
                $('#newItemTable').append('<tr id="nrow' + rowId + '"><td><input value="代金券"/></td><td data-type="2">代金券</td><td><input value="10"/></td><td>-</td><td><input value="10"/></td><td><a href="javascript:void(0);" onclick="onDeleteNewItemRow(\'nrow' + rowId + '\');">删除</a></td></tr>');
                var count = $('#newPackageItemCount').text();
                $('#newPackageItemCount').text(parseInt(count) + 1);
            });
            $('#btnQueryServiceItem').on('click', function () {
                queryServiceItemList();
            });
            $('#btnSubmitNew').on('click', function () {
                var title = null;
                if (g_editingPackage == null) {
                    title = "是否要创建新套餐?";
                } else {
                    title = "是否要修改套餐?";
                }
                if (window.confirm(title)) {
                    var entity = {};
                    entity.code = $('#newId').val();
                    entity.code = $('#newCode').val();
                    if (!entity.code) {
                        alert('编号不能为空');
                        return;
                    }
                    entity.name = $('#newName').val();
                    if (!entity.name) {
                        alert('名称不能为空');
                        return;
                    }
                    entity.startSaleTime = $('#newStartSaleTime').val();
                    entity.stopSaleTime = $('#newStopSaleTime').val();
                    entity.expiringDate = $('#newExpiringDate').val();
                    entity.packageAccessibility = $('#newPackageAccessibility').val();
                    entity.salesCommissionType = $('#newSalesCommissionType').val();
                    entity.salesCommission = $('#newSalesCommission').val();
                    entity.technicianCommissionType = $('#newTechnicianCommissionType').val();
                    entity.technicianCommission = $('#newTechnicianCommission').val();
                    entity.price = $('#newPrice').val();
                    entity.description = $('#newDescription').val();

                    var itemArray = [];
                    $('#newItemTable').find('tr').each(function (rIndex) {
                        if (rIndex == 0) {
                            return;
                        }
                        var item = {};
                        itemArray.push(item);
                        $(this).find('td').each(function (index) {
                            if (index == 0) {
                                item.name = $($(this).children()[0]).val();
                            } else if (index == 1) {
                                item.type = $(this).attr('data-type');
                                if (item.type == 1) {
                                    item.tagDataType = $(this).attr('target-type');
                                    item.tagData = $(this).attr('target-data');
                                }
                            } else if (index == 2 && item.type == 2) {
                                item.value = $($(this).children()[0]).val();
                            } else if (index == 3 && item.type == 1) {
                                item.value = $($(this).children()[0]).val();
                            } else if (index == 4) {
                                item.marketPrice = $($(this).children()[0]).val();
                            }
                        });
                    });
                    if (itemArray.length == 0) {
                        alert("需要添加套餐项目信息!");
                        return;
                    }
                    var priceArray = [];
                    $('#newPriceTable').find('tr').each(function (rIndex) {
                        if (rIndex == 0) {
                            return;
                        }
                        var item = {};
                        priceArray.push(item);
                        $(this).find('td').each(function (index) {
                            if (index == 1) {
                                var oInput = $($(this).children()[0]);
                                item.price = oInput.val();
                                item.channel = oInput.attr('data-code');
                            }
                        });
                    });
                    if (g_editingPackage == null) {
                        $.ajax({
                            type: "POST",
                            url: "@Url.Content("~/api/asms/Create/Package")",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            data: JSON.stringify({ 'package': entity, 'itemArray': itemArray, 'priceArray': priceArray }),
                            success: function (data) {
                                if (data.isSuccess) {
                                    alert('创建成功');
                                    $('#myModal').modal('hide');
                                    queryAdvertisementList();
                                } else {
                                    alert(data.message);
                                }
                            },
                            error: function (data) {
                                alert('error');
                            }
                        });
                    } else {
                        $.ajax({
                            type: "POST",
                            url: "@Url.Content("~/api/asms/Update/Package")",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            data: JSON.stringify({ 'id':g_editingPackage.package.id, 'package': entity, 'itemArray': itemArray, 'priceArray': priceArray }),
                            success: function (data) {
                                if (data.isSuccess) {
                                    alert('修改成功');
                                    $('#myModal').modal('hide');
                                    queryAdvertisementList();
                                } else {
                                    alert(data.message);
                                }
                            },
                            error: function (data) {
                                alert('error');
                            }
                        });
                    }
                }
            });

            queryAdvertisementList();
        });

        function queryPriceList() {
            if (oTablePrice) {
                oTablePrice.fnClearTable();
            }
            oTablePrice = $("#newPriceTable").dataTable({
                bRetrieve: true,
                bPaginate: false,
                "bLengthChange": false, //改变每页显示数据数量
                "iDisplayLength": 20,
                "bFilter": false, //过滤功能
                "bProcessing": false, // 是否显示取数据时的那个等待提示
                "bServerSide": true,//这个用来指明是通过服务端来取数据
                "sAjaxSource": "@Url.Content("~/API/ASMS/Query/MasterDataItemPagedList")",//这个是请求的地址
                "fnServerData": function (sSource, aoData, fnCallback) {
                    var start = 0;
                    var limit = 0;
                    var propertyNames = new Array();
                    var sortIndex = 0;
                    var sortDirection;
                    for (var index = 0; index < aoData.length; index++) {
                        if (aoData[index].name == 'iDisplayStart') {
                            start = aoData[index].value;
                        } else if (aoData[index].name == 'iDisplayLength') {
                            limit = aoData[index].value;
                            if (limit = -1) {
                                limit = ASMS.Constant.PageAllLimit;
                            }
                        } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                            propertyNames.push(aoData[index].value);
                        } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                            sortIndex = aoData[index].value;
                        } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                            sortDirection = aoData[index].value;
                        }
                    }
                    $.ajax({
                        url: sSource,//这个就是请求地址对应sAjaxSource
                        data: {
                            'MasterDataCode': 'SALESCHANNEL',
                            'start': start,
                            'limit': limit
                        },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                        type: 'get',
                        dataType: 'json',
                        async: false,
                        success: function (result) {
                            if (result.isSuccess) {
                                fnCallback({
                                    sEcho: aoData[0].value,
                                    iTotalRecords: result.data.total,
                                    iTotalDisplayRecords: result.data.total,
                                    aaData: result.data.items
                                });
                            }
                        },
                        error: function (msg) {
                        }
                    });
                },
                "aoColumns": [
                    {
                        'sWidth': '120px',
                        mData: "name",
                        bSortable: false
                    },
                    {
                        mData: null,
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            if (g_editingPackage == null) {
                                return '<input data-code="' + full.code + '" value="0"/>';
                            } else {
                                for (var priceIndex = 0; priceIndex < g_editingPackage.prices.length; priceIndex++) {
                                    if (g_editingPackage.prices[priceIndex].channel == full.code) {
                                        return '<input data-code="' + full.code + '" value="' + g_editingPackage.prices[priceIndex].price + '"/>';
                                    }
                                }
                                return '<input data-code="' + full.code + '" value="0"/>';
                            }
                        }
                    }
                ]
                //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
            });
        };

        function queryServiceItemList() {
            if (oTableServiceItem) {
                oTableServiceItem.fnClearTable();
            }
            oTableServiceItem = $("#itemTable").dataTable({
                bRetrieve: true,
                "bLengthChange": false, //改变每页显示数据数量
                "iDisplayLength": 10,
                "bFilter": false, //过滤功能
                "bProcessing": false, // 是否显示取数据时的那个等待提示
                "bServerSide": true,//这个用来指明是通过服务端来取数据
                "sAjaxSource": "@Url.Content("~/API/ASMS/Query/ServiceItemPagedList")",//这个是请求的地址
                "fnServerData": function (sSource, aoData, fnCallback) {
                    var start = 0;
                    var limit = 0;
                    var propertyNames = new Array();
                    var sortIndex = 0;
                    var sortDirection;
                    for (var index = 0; index < aoData.length; index++) {
                        if (aoData[index].name == 'iDisplayStart') {
                            start = aoData[index].value;
                        } else if (aoData[index].name == 'iDisplayLength') {
                            limit = aoData[index].value;
                        } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                            propertyNames.push(aoData[index].value);
                        } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                            sortIndex = aoData[index].value;
                        } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                            sortDirection = aoData[index].value;
                        }
                    }
                    $.ajax({
                        url: sSource,//这个就是请求地址对应sAjaxSource
                        data: {
                            "filter": $('#editServiceItemFilter').val(),
                            "status": 1,
                            categoryId: null,
                            'businessId': null,
                            'stationId': null,
                            'start': start,
                            'limit': limit
                        },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                        type: 'get',
                        dataType: 'json',
                        async: false,
                        success: function (result) {
                            if (result.isSuccess) {
                                fnCallback({
                                    sEcho: aoData[0].value,
                                    iTotalRecords: result.data.total,
                                    iTotalDisplayRecords: result.data.total,
                                    aaData: result.data.items
                                });
                            }
                        },
                        error: function (msg) {
                        }
                    });
                },
                "aoColumns": [
                    {
                        mData: "code",
                        bSortable: false
                    },
                    {
                        mData: "name",
                        bSortable: false
                    },
                    {
                        mData: "price",
                        bSortable: false
                    },
                    {
                        //"sTitle": "Operation",
                        mData: null,
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            return '<div class="btn-group"><a href="javascript:void(0)" onclick="onNewServiceItemRow(this);" data-id="' + full.id + '" data-name="' + full.name + '" data-price="' + full.price + '" class="btn btn-xs btn-info"><i class="icon-ok bigger-100"></i>选择</a></div>';
                        }
                    }
                ]
                //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
            });
        };

        function queryAdvertisementList() {
            if (oTable) {
                oTable.fnClearTable();
            }
            oTable = $("#businessTable").dataTable({
                bRetrieve: true,
                "bLengthChange": false, //改变每页显示数据数量
                "iDisplayLength": 20,
                "bFilter": false, //过滤功能
                "bProcessing": false, // 是否显示取数据时的那个等待提示
                "bServerSide": true,//这个用来指明是通过服务端来取数据
                "sAjaxSource": "@Url.Content("~/API/ASMS/Query/PackagePagedList")",//这个是请求的地址
                "fnServerData": retrieveData, // 获取数据的处理函数
                "aoColumns": [
                    {
                        //"sTitle": "Name",
                        "sWidth": '40px',
                        mData: null,
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            return '<input type="checkbox" name="selectItem" data-id="' + full.id + '"/>';
                        }
                    },
                    {
                        mData: "code",
                        bSortable: false
                    },
                    {
                        mData: "name",
                        bSortable: false
                    },
                    {
                        mData: "price",
                        bSortable: false
                    },
                    {
                        mData: "marketPrice",
                        bSortable: false
                    },
                    {
                        bSortable: false,
                        mData: "status",
                        mRender: function (data, type, full) {
                            if (data == 1) {
                                return "正常";
                            } else if (data == 2) {
                                return "已禁用";
                            } else if (data == 3) {
                                return "已删除";
                            }
                            return 'unknown';
                        }
                    },
                    {
                        mData: "createdTime",
                        bSortable: false
                    },
                    {
                        //"sTitle": "Operation",
                        mData: null,
                        "sWidth": '200px',
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            if (full.status == 3) {
                                return "";
                            }
                            return '<div class="btn-group"><a href="javascript:void(0)" onclick="onEdit(this);" data-id="' + full.id + '" class="btn btn-xs btn-info"><i class="icon-pencil bigger-100"></i>修改</a><a href="javascript:void(0)" onclick="onDelete(this);" data-id="' + full.id + '" class="btn btn-xs btn-danger"><i class="icon-remove bigger-100"></i>删除</a></div>';
                        }
                    }
                ]
                //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
            });
        };

        // 3个参数的名字可以随便命名,但必须是3个参数,少一个都不行
        function retrieveData(sSource, aoData, fnCallback) {
            var start = 0;
            var limit = 0;
            var propertyNames = new Array();
            var sortIndex = 0;
            var sortDirection;
            for (var index = 0; index < aoData.length; index++) {
                if (aoData[index].name == 'iDisplayStart') {
                    start = aoData[index].value;
                } else if (aoData[index].name == 'iDisplayLength') {
                    limit = aoData[index].value;
                } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                    propertyNames.push(aoData[index].value);
                } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                    sortIndex = aoData[index].value;
                } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                    sortDirection = aoData[index].value;
                }
            }
            $.ajax({
                url: sSource,//这个就是请求地址对应sAjaxSource
                data: {
                    "filter": $('#com-search-input').val(),
                    "status": $('#query-status').val(),
                    'start': start,
                    'limit': limit
                },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                type: 'get',
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.isSuccess) {
                        fnCallback({
                            sEcho: aoData[0].value,
                            iTotalRecords: result.data.total,
                            iTotalDisplayRecords: result.data.total,
                            aaData: result.data.items
                        });
                    }
                },
                error: function (msg) {
                }
            });
        }
        
        function onEdit(el) {
            var id = $(el).attr('data-id');
            if (!id) {
                return;
            }
            $("#newItemTable tr:gt(0)").remove();
            $('#formNew')[0].reset();
            $('#myModal').modal();
            $('#myModalLabel').text('修改套餐');
            $.ajax({
                url: "@Url.Content("~/API/ASMS/Get/PackageDetail")",
                data: {
                    'id':id
                },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                type: 'get',
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.isSuccess) {
                        g_editingPackage = result.data;
                        $('#newCode').prop('disabled', 'disabled');
                        $('#newId').val(result.data.package.id);
                        $('#newCode').val(result.data.package.code);
                        $('#newName').val(result.data.package.name);
                        $('#newStartSaleTime').val(result.data.package.startSaleTime);
                        $('#newStopSaleTime').val(result.data.package.stopSaleTime);
                        $('#newExpiringDate').val(result.data.package.expiringDate);
                        $('#newPackageAccessibility').val(result.data.package.packageAccessibility);
                        $('#newSalesCommissionType').val(result.data.package.salesCommissionType);
                        $('#newSalesCommission').val(result.data.package.salesCommission);
                        $('#newTechnicianCommissionType').val(result.data.package.technicianCommissionType);
                        $('#newTechnicianCommission').val(result.data.package.technicianCommission);
                        $('#newPrice').val(result.data.package.price);
                        $('#newDescription').val(result.data.package.description);

                        $('#newPackageItemCount').text(result.data.items.length);
                        for (var index = 0; index < result.data.items.length; index++) {
                            var rowId = g_rowIndex++;
                            var item = result.data.items[index];
                            if (item.type == 1) {
                                // 服务项目
                                $('#newItemTable').append('<tr id="nrow' + rowId + '"><td><input value="' + item.name + '"/></td><td data-type="1" target-type="1" target-data="' + item.tagData + '">服务项目</td><td>-</td><td><input value="' + item.value + '"/></td><td><input value="' + item.marketPrice + '"/></td><td><a href="javascript:void(0);" onclick="onDeleteNewItemRow(\'nrow' + rowId + '\');">删除</a></td></tr>');
                            } else {
                                $('#newItemTable').append('<tr id="nrow' + rowId + '"><td><input value="' + item.name + '"/></td><td data-type="2">代金券</td><td><input value="' + item.value + '"/></td><td>-</td><td><input value="' + item.marketPrice + '"/></td><td><a href="javascript:void(0);" onclick="onDeleteNewItemRow(\'nrow' + rowId + '\');">删除</a></td></tr>');
                            }
                        }
                        queryPriceList();
                    }
                },
                error: function (msg) {
                }
            });
        }

        function onDelete(el) {
            var id = $(el).attr('data-id');  // save
            if (!id) {
                return;
            }
            if (window.confirm("是否要删除套餐?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/Delete/Package")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'id': id }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };
        function onDeleteMany(idArray) {
            if (idArray == null || idArray.length == 0)
                return;
            if (window.confirm("是否要删除选中的套餐?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/Delete/PackageArray")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'idArray': idArray }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        }

        function onDeleteNewItemRow(newRowId) {
            $('#' + newRowId).remove();
            var count = $('#newPackageItemCount').text();
            $('#newPackageItemCount').text(parseInt(count) - 1);
        }
        function onNewServiceItemRow(el) {
            $('#pickerServiceItem').modal('hide');
            var id = $(el).attr('data-id');
            var name = $(el).attr('data-name');
            var price = $(el).attr('data-price');

            var rowId = g_rowIndex++;
            $('#newItemTable').append('<tr id="nrow' + rowId + '"><td><input value="' + name + '"/></td><td data-type="1" target-type="1" target-data="' + id + '">服务项目</td><td>-</td><td><input value="1"/></td><td><input value="' + price + '"/></td><td><a href="javascript:void(0);" onclick="onDeleteNewItemRow(\'nrow' + rowId + '\');">删除</a></td></tr>');
            var count = $('#newPackageItemCount').text();
            $('#newPackageItemCount').text(parseInt(count) + 1);
        }
    </script>
}
